name: 'Local Action'
description: 'Runs a composite step action'
inputs:
  version-proposed:
    required: true
    description: "Version proposed"

outputs:
  bump-type:
    description: "The bump type. One of: ['major', 'minor', 'patch']"
    value: ${{ steps.get-bump-type.outputs.bump-type }}

runs:
  using: "composite"
  steps:

    - name: Pip installs
      shell: bash
      run: |
        pipx install --include-deps bumpversion

    - name: Detect current version and release version
      id: detect-versions
      shell: bash
      run: |
        echo "Detecting current version number..."
        CURR_VER="$(bumpversion major --allow-dirty --dry-run --list | grep current_version= | cut -d'=' -f2)"
        RELEASE_VER="${CI_COMMIT_REF_NAME#release/v}"
        echo "Detected current version '$CURR_VER' and release version '$RELEASE_VER'"
        echo "current-version=$CURR_VER" >> $GITHUB_OUTPUT
        echo "release-version=$RELEASE_VER" >> $GITHUB_OUTPUT

    - name: Get bump type
      id: get-bump-type
      shell: bash
      run: |
        CURR_VER=${{ steps.detect-versions.outputs.current-version }}
        NEW_VER=${{ inputs.version-proposed }}
        [[ "$CURR_VER" == "" ]] && echo "Could not detect current version" && exit 1
        [[ "$NEW_VER" == "" ]] && echo "Could not detect release version" && exit 1
        [[ "$NEW_VER" == "$CURR_VER" ]] && echo "Version is already bumped. Aborting job." && exit 0
        MAJOR_BUMP="$(bumpversion major --allow-dirty --dry-run --list | grep new_version= | cut -d'=' -f2)"
        MINOR_BUMP="$(bumpversion minor --allow-dirty --dry-run --list | grep new_version= | cut -d'=' -f2)"
        PATCH_BUMP="$(bumpversion patch --allow-dirty --dry-run --list | grep new_version= | cut -d'=' -f2)"
        [[ $NEW_VER = $MAJOR_BUMP ]] && BUMP_TYPE="major"
        [[ $NEW_VER = $MINOR_BUMP ]] && BUMP_TYPE="minor"
        [[ $NEW_VER = $PATCH_BUMP ]] && BUMP_TYPE="patch"
        echo "bump-type=$BUMP_TYPE" >> $GITHUB_OUTPUT

    - name: Validate version bump
      id: validate-version-bump
      shell: bash
      run: |
        CURR_VER=${{ steps.detect-versions.outputs.current-version }}
        NEW_VER=${{ inputs.version-proposed }}
        BUMP_TYPE=${{ steps.get-bump-type.outputs.bump-type }}
        [[ "$BUMP_TYPE" == "" ]] && echo "Could not detect version bump type between '$CURR_VER' and '$NEW_VER'" && exit 1
        echo "Detected bump type '$BUMP_TYPE' from '$CURR_VER' to '$NEW_VER'"
